package cn.zhyjohn.paysystem.biz;

import cn.zhyjohn.paysystem.bean.AliPayBean;
import cn.zhyjohn.paysystem.common.CreateQRCode;
import cn.zhyjohn.paysystem.common.Response;
import cn.zhyjohn.paysystem.requset.AliPayParams;
import cn.zhyjohn.paysystem.requset.AliTradePayParams;
import cn.zhyjohn.paysystem.requset.AliTradePrecreateParams;
import cn.zhyjohn.paysystem.requset.AliTradeRefundParams;
import cn.zhyjohn.paysystem.service.AlipayService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author JOHN
 * @title: AlipayManager
 * @projectName paysystem
 * @since 2019/8/28
 */
@Component
public class AlipayManager {

    @Autowired
    private AlipayService alipayDrud;

    @Autowired
    private AliPayBean aliPayBean;

    /**
     * 支付宝app支付
     * @author JOHN
     * @param params
     * @date 11:43
     **/
    public Response appPay(AliPayParams params) {
        return alipayDrud.appPay(params);
    }

    /**
     * 支付宝Wap支付
     * @author JOHN
     * @date 11:46
     * @param params
     **/
    public Response wapPay(HttpServletResponse response, AliPayParams params) {
        return alipayDrud.wapPay(response, params);
    }

    /**
     * 支付宝PC支付
     * @author JOHN
     * @date 11:53
     * @param params
     **/
    public Response pcPay(HttpServletResponse response, AliPayParams params) {
        return alipayDrud.pcPay(response, params);
    }

    /**
     * 支付宝条码/声波支付
     * @author JOHN
     * @date 11:59
     * @param params
     **/
    public Response tradePay(AliTradePayParams params) {
        return alipayDrud.tradePay(params);
    }

    /**
     * 支付宝扫码支付
     * @author JOHN
     * @date 12:03
     * @param precreatePay
     **/
    public Response tradePrecreatePay(HttpServletResponse response, AliTradePrecreateParams precreatePay) throws IOException {
        Response resp = alipayDrud.tradePrecreatePay(precreatePay);
        if (resp.isSuccess()) {
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/png");
            CreateQRCode.encoderQRCode(resp.getData().toString(),response.getOutputStream());
            return null;
        }
        return resp;
    }

    /**
     * 支付宝单笔转账到支付宝账户
     * @author JOHN
     * @date 13:39
     * @param params
     **/
    public Response transfer(AliPayParams params) {
        return alipayDrud.transfer(params);
    }

    /**
     * 支付宝退款
     * @author JOHN
     * @date 13:49
     * @param params
     **/
    public Response tradeRefund(AliTradeRefundParams params) {
        return alipayDrud.tradeRefund(params);
    }

    /**
     * 支付宝-通知回调
     * @author JOHN
     * @date 16:34
     **/
    public String notifyUrl(HttpServletRequest request) {
        // 获取支付宝POST过来反馈信息
        Map<String, String> map = alipayDrud.callback(request);
        try {
            boolean verifyResponse = AlipaySignature.rsaCheckV1(map, aliPayBean.getPrivateKey(), "UTF-8","RSA2");
            if (verifyResponse) {
                // TODO 商户的业务逻辑程序代码
                return "success";
            } else {
                return "failure";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return "failure";
        }
    }

    /**
     * 支付宝-返回回调
     * @author JOHN
     * @date 16:34
     **/
    public String returnUrl(HttpServletRequest request) {
        // 获取支付宝GET过来反馈信息
        Map<String, String> map = alipayDrud.callback(request);
        try {
            boolean verifyResponse = AlipaySignature.rsaCheckV1(map, aliPayBean.getPublvicKey(), "UTF-8","RSA2");
            if (verifyResponse) {
                return "success";
            } else {
                return "failure";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return "failure";
        }
    }
}
